#ifndef H_VECTOR
#define H_VECTOR

#include <math.h>
#include <iostream>
#include "debug.h"

class Vector {
   private:
      float* x;
      float* y;

      void alloc () {	      //#D
	 x = new float;
	 y = new float;
      }
   public:
      Vector () {	   //#D
	 alloc ();
	 this->set(0.,0.); 
      }

      Vector ( const Vector & vec) {	     //#D
	 float ax,ay;
	 ax = vec.getx();
	 ay = vec.gety();
	 if ( this != &vec ) {
	    alloc ();
	 }	 
	 *x=ax;
	 *y=ay;
      }

      Vector (float _x, float _y) {	     //#D
	 alloc ();
	 this->set(_x,_y); 
      }

      ~Vector () {	   //#D
	 delete x;
	 delete y;
      }

      float getx () const { return *x; }
      float gety () const { return *y; }

      float abs2 () const { return (*x)*(*x)+(*y)*(*y); }
      float abs () const { return sqrt ( (*x)*(*x)+(*y)*(*y) ); }

      void set ( const float & _x, const float & _y ) { *x = _x; *y = _y; }

      const Vector versor () const {
	 float mod = abs();
	 return Vector (*x/mod,*y/mod);
      }

      const Vector perpVersor () const {
	 float mod = abs();
	 return Vector (*y/mod,*x/mod*(-1.));
      }

      Vector & operator= ( const Vector& vec ) {	 //#D
	 if (this == &vec ) return *this;
	 this->set ( vec.getx (), vec.gety () );
	 return *this;
      }

      const float operator* ( const Vector & vec ) const {	  //#CD
	 return (*x)*vec.getx()+(*y)*vec.gety();
      }

      Vector & operator*= ( const float & f ) {	      //#D
	 *x *= f;
	 *y *= f;
	 return *this;
      }

      Vector & operator+= ( const Vector & vec ) {	 //#D
	 *x += vec.getx ();
	 *y += vec.gety ();
	 return *this;
      }

      Vector & operator-= ( const Vector & vec ) {	 //#D
	 *x -= vec.getx ();
	 *y -= vec.gety ();
	 return *this;
      }

      const Vector operator* ( const float & f ) const {       //#CD
	 return Vector ( *this ) *= f;
      }

      const Vector operator+ ( const Vector & vec ) const {	  //#CD
	 return Vector ( *this ) += vec;
      }

      const Vector operator- ( const Vector & vec ) const {	  //#CD
	 return Vector ( *this ) -= vec;
      }

      void setx ( const float _x ) {
	 *x = _x;
      }

      void sety ( const float _y ) {
	 *y = _y;
      }

      void show () const { std::cout << *x << " " << *y << "\n"; }
};

#endif
